#include "MonitoringDeviceAdd.h"
#include "ui_MonitoringDeviceAdd.h"

MonitoringDeviceAdd::MonitoringDeviceAdd(QWidget *parent,userModel*model) :
    QWidget(parent),
    ui(new Ui::MonitoringDeviceAdd),
    model(model)
{
    ui->setupUi(this);
    qDebug()<<"id22"<<model->id;
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        qDebug()<<"id=="<<model->id;
        QJsonObject jsonObj;
        jsonObj["action"] ="addDevice";
        jsonObj["user_id"] =QString::number( model->id);
        jsonObj["name"] =ui->lineEdit_0->text();
        jsonObj["type"] =ui->lineEdit_1->text();
        jsonObj["location"] =ui->lineEdit_2->text();
        jsonObj["manufacturer"] =ui->lineEdit_3->text();
        jsonObj["model"] =ui->lineEdit_4->text();
        jsonObj["status"] =ui->lineEdit_5->text();
        qDebug()<<"==="<<jsonObj;
        LOG_INFO("用户"+model->id+"添加了设备");
        if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
            //  qDebug() << "Login data sent successfully";
        } else {
            ui->label_2->setText("登录失败，请检查网络");
            return ;
        }
        connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringDeviceAdd::handleDataReceived);
    });
}
void MonitoringDeviceAdd::handleDataReceived(const QByteArray &data)
{

    qDebug()<<"==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="addDevice"){
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        if(code==QUERY_SUCCESS){
            ui->label_2->setText("添加成功");

        }else {
            ui->label_2->setText("添加失败");
        }

    }
}
MonitoringDeviceAdd::~MonitoringDeviceAdd()
{
    delete ui;
}
